﻿using System;
using WindowsLive;
using WindowsLive.Writer;
using WindowsLive.Writer.Api;
using System.Text;
using System.Text.RegularExpressions;
using FlickrNet;
using System.Windows.Forms;
using System.ComponentModel;

namespace TrailImage
{
	[InsertableContentSource("Haiku"),
	 WriterPlugin("B0B3DF60-9E11-11E1-BDDB-82FE6088709B", "Haiku",
		PublisherUrl = "http://idaho-adventure.com",
		Description = "Plugin for retrieving Flickr images",
		HasEditableOptions = false)]
	public class HaikuPlugin : PluginBase
	{
		/// <summary>
		/// Create content for LiveWriter
		/// </summary>
		public override DialogResult CreateContent(IWin32Window win, ref string selectedText)
		{
			var result = DialogResult.Cancel;

			using (var form = new InsertHaikuForm())
			{
				result = this.Process(form, f => this.BuildHaikuHTML(f), win);
			}
			selectedText = FlickrPlugin.Selection;
			return result;
		}

		string BuildHaikuHTML(InsertHaikuForm form)
		{
			var text = form.PoemBox.Text;

			text = text.Replace("'","’");
			text = text.Replace(Environment.NewLine, "<br/>");

			var startTag = form.WrapTagText.Text.Replace("/", "");
			var endTag = "<" + startTag.Substring(1, startTag.IndexOf(' ')) + "/>";

			return startTag + text + endTag;
		}
	}
}